/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package seGobangClient.uiSystem2D;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import seGobangClient.onlineHall.Desk;
import seGobangClient.onlineHall.PlayerInfo;
import seGobangClient.onlineHall.Room;
import seGobangResources.PathRes;

/**
 *
 * @author Leo
 */
public class OnlineScene
{

    private PathRes path = new PathRes();
    private Font font;
    private JTextField name;
    private JPasswordField password;
    private JLabel loginB;
    private JLabel quit;
    private JLabel regist;
    private JFrame frame;
    private getpane pane;
    private JList roomList;
    private JLabel in;
    private JLabel back;
    private ArrayList<String> rooms = new ArrayList<String>();
    private JLabel seatsleft[];
    private JLabel seatsright[];
    private ImageIcon playerImageA;
    private ImageIcon playerImageB;
    private JLabel backtoroomlist;
    private int deskstate[];
    private int base = 1;
    private JLabel roomnameL;
    private JLabel deskidL;
    private JLabel player1;
    private JLabel player2;
    private JLabel desk;
    private JLabel readyornot;
    private JLabel ready;
    private getpane playerPane;
    private JLabel backtoroom;
    private JLabel playerInfo;
    private boolean localready = false;
    private int deskid;
    private JLabel olplayer;
    private JLabel win;
    private JLabel lose;

    public void initLoginScene(final JFrame container, final getpane mainPane)
    {
        frame = container;
        pane = mainPane;
        container.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        try
        {
            mainPane.imgpath = path.getPath("Texture/LoginBack");
            playerImageA = new ImageIcon(path.getPath("Texture/PlayerA"));
            playerImageB = new ImageIcon(path.getPath("Texture/PlayerB"));
            mainPane.remove(mainPane.getComponentAt(535, 0));
        } catch (Exception e)
        {

        }

        name = new JTextField();
        password = new JPasswordField();
        loginB = new JLabel("登录");
        quit = new JLabel("返回");
        regist = new JLabel("注册");
        font = new Font("华文行楷", Font.PLAIN, 25);

        name.setSize(150 * base, 25 * base);
        name.setLocation(340 * base, 173 * base);

        password.setSize(150 * base, 25 * base);
        password.setLocation(340 * base, 220 * base);
        password.setEchoChar('*');

        loginB.setSize(50 * base, 30 * base);
        loginB.setLocation(280 * base, 280 * base);
        loginB.setFont(font);
        loginB.setForeground(Color.white);
        loginB.setCursor(new Cursor(Cursor.HAND_CURSOR));
        quit.setSize(50 * base, 30 * base);
        quit.setLocation(450 * base, 280 * base);
        quit.setFont(font);
        quit.setForeground(Color.white);
        quit.setCursor(new Cursor(Cursor.HAND_CURSOR));
        loginB.addMouseListener(new MouseAdapter()
        {

            public void mouseClicked(MouseEvent e)
            {
                System.out.println(new String(password.getPassword()));
                SceneManager.MyCurrentController.doEvent("TryLogin", new String[]{name.getText().trim(), new String(password.getPassword())});

                frame.repaint();
            }
            });
        quit.addMouseListener(new MouseAdapter()
        {

            public void mouseClicked(MouseEvent e)
            {
                SceneManager.MyCurrentController.doEvent("ExitHall", null);
                frame.repaint();
            }
            });
        regist.setSize(50 * base, 30 * base);
        regist.setLocation(365 * base, 280 * base);
        regist.setFont(font);
        regist.setForeground(Color.white);
        regist.setCursor(new Cursor(Cursor.HAND_CURSOR));
        regist.addMouseListener(new MouseAdapter()
        {

            public void mouseClicked(MouseEvent e)
            {
                try
                {
                    BrowserControl.displayURL("http://59.78.37.65:8084/RegisterProject/register.jsp");
                } catch (Exception ex)
                {
                }
            }
            });
        mainPane.add(name);
        mainPane.add(password);
        mainPane.add(loginB);
        mainPane.add(quit);
        mainPane.add(regist);
        container.repaint();
    }

    public void Loginclear()
    {
        try
        {
            pane.remove(name);
            pane.remove(password);
            pane.remove(loginB);
            pane.remove(quit);
            pane.remove(regist);
        } catch (Exception e)
        {

        }
    }

    public void initRoomChooseScene(final JFrame container, final getpane mainPane)
    {
        pane = mainPane;
        container.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        try
        {
            mainPane.imgpath = path.getPath("Texture/RoomListBack");
        } catch (Exception e)
        {

        }
        font = new Font("华文行楷", Font.PLAIN, 19);
        roomList = new JList();
        roomList.setSize(285 * base, 100 * base);
        roomList.setLocation(190 * base, 190 * base);
        roomList.setFont(font);

        font = new Font("华文行楷", Font.PLAIN, 25);
        in = new JLabel("进入");
        in.setSize(50 * base, 30 * base);
        in.setLocation(350 * base, 310 * base);
        in.setFont(font);
        in.setForeground(Color.white);
        in.setCursor(new Cursor(Cursor.HAND_CURSOR));
        in.addMouseListener(new MouseAdapter()
        {

            public void mouseClicked(MouseEvent e)
            {
                if (roomList.getSelectedIndex() != -1)
                {
                    SceneManager.MyCurrentController.doEvent("JoinRoom", roomList.getSelectedIndex());
                    frame.repaint();
                }
            }
        });
        back = new JLabel("返回");
        back.setSize(50 * base, 30 * base);
        back.setLocation(430 * base, 310 * base);
        back.setFont(font);
        back.setForeground(Color.white);
        back.setCursor(new Cursor(Cursor.HAND_CURSOR));
        back.addMouseListener(new MouseAdapter()
        {

            public void mouseClicked(MouseEvent e)
            {
                SceneManager.MyCurrentController.doEvent("ExitHall", null);
            }
        });
        mainPane.add(roomList);
        mainPane.add(in);
        mainPane.add(back);
        SceneManager.MyCurrentController.doEvent("UpdateRoomList", null);
        mainPane.repaint();
    }

    public void RoomChooseclear()
    {
        try
        {
            roomList.setVisible(false);
            pane.remove(roomList);
            roomList = null;
            pane.remove(in);
            pane.remove(back);
        } catch (Exception e)
        {

        }
    }

    public void refreshRoom(Room[] Rooms)
    {
        rooms.clear();
        for (int i = 0; i < Rooms.length; i++)
        {
            rooms.add(Rooms[i].RoomName + "(" + Rooms[i].RoomDesc + ")");
        }
        roomList.setListData(rooms.toArray());
        roomList.repaint();
    }

    public void initRoomScene(final JFrame container, final getpane mainPane, String roomname)
    {
        pane = mainPane;
        try
        {
            mainPane.imgpath = path.getPath("Texture/RoomBack");
        } catch (Exception e)
        {

        }
        seatsleft = new JLabel[12];
        seatsright = new JLabel[12];
        deskstate = new int[12];
        roomnameL = new JLabel(roomname);
        roomnameL.setSize(120 * base, 35 * base);
        font = new Font("华文行楷", Font.PLAIN, 35);
        roomnameL.setFont(font);
        roomnameL.setForeground(Color.white);
        roomnameL.setLocation(35, 20);
        backtoroomlist = new JLabel("返回");
        font = new Font("华文行楷", Font.PLAIN, 25);
        backtoroomlist.setForeground(Color.white);
        backtoroomlist.setFont(font);
        backtoroomlist.setCursor(new Cursor(Cursor.HAND_CURSOR));
        backtoroomlist.setSize(80 * base, 30 * base);
        backtoroomlist.setLocation(550 * base, 20 * base);
        backtoroomlist.addMouseListener(new MouseAdapter()
        {

            public void mouseClicked(MouseEvent e)
            {
                SceneManager.MyCurrentController.doEvent("ExitRoom", null);
                frame.repaint();
            }
            });
        mainPane.add(backtoroomlist);
        mainPane.add(roomnameL);
        try
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    final int index = i * 4 + j;

                    seatsleft[i * 4 + j] = new JLabel();
                    seatsleft[i * 4 + j].setSize(40 * base, 53 * base);
                    seatsleft[i * 4 + j].setLocation((j * 154 + 24) * base, (i * 113 + 115) * base);
                    seatsleft[i * 4 + j].setCursor(new Cursor(Cursor.HAND_CURSOR));
                    seatsleft[i * 4 + j].addMouseListener(new MouseAdapter()
                    {

                        public void mouseClicked(MouseEvent e)
                        {
                            if (deskstate[index] == 0)
                            {
                                SceneManager.MyCurrentController.doEvent("JoinDesk", index + 1);
                            }
                        }
                    });
                    seatsright[i * 4 + j] = new JLabel();
                    seatsright[i * 4 + j].setSize(40 * base, 53 * base);
                    seatsright[i * 4 + j].setLocation((j * 154 + 24 + 100) * base, (i * 113 + 115) * base);
                    seatsright[i * 4 + j].setCursor(new Cursor(Cursor.HAND_CURSOR));
                    seatsright[i * 4 + j].addMouseListener(new MouseAdapter()
                    {

                        public void mouseClicked(MouseEvent e)
                        {
                            if (deskstate[index] == 0 || deskstate[index] == 1)
                            {
                                SceneManager.MyCurrentController.doEvent("JoinDesk", index + 1);
                            }
                        }
                    });
                    mainPane.add(seatsleft[i * 4 + j]);
                    mainPane.add(seatsright[i * 4 + j]);
                }
            }
        } catch (Exception e)
        {

        }
        mainPane.repaint();
    }

    public void refreshDeskState(Desk[] Desks)
    {
        for (int i = 1; i < Desks.length; i++)
        {
            if (Desks[i].getState() == 0)
            {
                deskstate[i - 1] = 0;
            } else if (Desks[i].getState() == 1)
            {
                seatsleft[i - 1].setIcon(playerImageA);
                seatsright[i - 1].setIcon(null);
                deskstate[i - 1] = 1;
            } else if (Desks[i].getState() == 2)
            {
                seatsleft[i - 1].setIcon(playerImageA);
                seatsright[i - 1].setIcon(playerImageB);
                deskstate[i - 1] = 2;
            } else if (Desks[i].getState() == 4)
            {
                seatsleft[i - 1].setIcon(playerImageA);
                seatsright[i - 1].setIcon(playerImageB);
                deskstate[i - 1] = 4;
            }
            seatsleft[i - 1].repaint();
            seatsright[i - 1].repaint();
        }
        refreshdesk();
    }

    public void Roomclear()
    {
        try
        {
            for (int i = 0; i < 12; i++)
            {
                pane.remove(seatsleft[i]);
                pane.remove(seatsright[i]);
            }
            roomnameL.setVisible(false);
            pane.remove(backtoroomlist);
        } catch (Exception e)
        {

        }
    }

    public void initDeskScene(final JFrame container, final getpane mainPane, int id)
    {
        deskid = id;
        pane = mainPane;
        try
        {
            mainPane.imgpath = path.getPath("Texture/DeskBack");
        } catch (Exception e)
        {

        }
        roomnameL.setVisible(true);
        deskidL = new JLabel("第" + id + "桌");
        deskidL.setForeground(Color.white);
        deskidL.setFont(font);
        deskidL.setSize(80 * base, 35 * base);
        deskidL.setLocation(450 * base, 400 * base);
        mainPane.add(deskidL);
        backtoroom = new JLabel("返回");
        backtoroom.setCursor(new Cursor(Cursor.HAND_CURSOR));
        backtoroom.setForeground(Color.white);
        backtoroom.setFont(font);
        backtoroom.setSize(50 * base, 35 * base);
        backtoroom.setLocation(540 * base, 400 * base);
        mainPane.add(backtoroom);
        backtoroom.addMouseListener(new MouseAdapter()
        {

            public void mouseClicked(MouseEvent e)
            {
                SceneManager.MyCurrentController.doEvent("ExitDesk", null);
                frame.repaint();
            }
            });

        playerPane = new getpane(420 * base, 309 * base, "");
        playerPane.setLayout(null);
        playerPane.setSize(420 * base, 309 * base);
        playerPane.setLocation(220 * base, 0);
        mainPane.add(playerPane);
        olplayer = new JLabel();
        olplayer.setForeground(Color.white);
        olplayer.setFont(font);
        olplayer.setSize(100 * base, 35 * base);
        olplayer.setLocation(80 * base, 27 * base);
        olplayer.setVisible(false);
        playerPane.add(olplayer);

        win = new JLabel();
        win.setForeground(Color.white);
        win.setFont(font);
        win.setSize(50 * base, 35 * base);
        win.setLocation(80 * base, 57 * base);
        win.setVisible(false);
        playerPane.add(win);

        lose = new JLabel();
        lose.setForeground(Color.white);
        lose.setFont(font);
        lose.setSize(50 * base, 35 * base);
        lose.setLocation(170 * base, 57 * base);
        lose.setVisible(false);
        playerPane.add(lose);

        ready = new JLabel("准备");
        ready.setForeground(Color.white);
        ready.setFont(font);
        ready.setSize(120 * base, 35 * base);
        ready.setLocation(35 * base, 400 * base);
        ready.setCursor(new Cursor(Cursor.HAND_CURSOR));
        mainPane.add(ready);
        ready.addMouseListener(new MouseAdapter()
        {

            public void mouseClicked(MouseEvent e)
            {
                if (!localready)
                {
                    localready = !localready;
                    SceneManager.MyCurrentController.doEvent("GetReady", null);
                    ready.setText("取消准备");
                } else
                {
                    localready = !localready;
                    SceneManager.MyCurrentController.doEvent("CancelReady", null);
                    ready.setText("准备");
                }
                frame.repaint();
            }
            });
        container.repaint();
    }

    public void refreshPlayerInfo(PlayerInfo info)
    {
        // playerInfo.setText("<html>" + info.Name + "<br>胜场数：" + info.Wins + "<br>负场数：" + info.Losts);
        olplayer.setText(info.Name);
        win.setText("" + info.Wins);
        lose.setText("" + info.Losts);
        playerPane.repaint();
    }

    public void refreshdesk()
    {
        try
        {
            if (deskstate[deskid - 1] == 3 && !localready)
            {
                playerPane.imgpath = path.getPath("Texture/DeskRaise");
                olplayer.setVisible(true);
                win.setVisible(true);
                lose.setVisible(true);
            }
            if (deskstate[deskid - 1] == 2)
            {
                playerPane.imgpath = path.getPath("Texture/DeskNoRaise");
                olplayer.setVisible(true);
                win.setVisible(true);
                lose.setVisible(true);
            }
            if (deskstate[deskid - 1] == 1)
            {
                playerPane.imgpath = "";
                olplayer.setVisible(false);
                win.setVisible(false);
                lose.setVisible(false);
            }
        } catch (Exception e)
        {

        }
        frame.repaint();
    }

    public void Deskclear()
    {
        pane.remove(deskidL);
        pane.remove(backtoroom);
        pane.remove(ready);
        pane.remove(playerPane);
        roomnameL.setVisible(false);
    }

    public void clear()
    {
        try
        {
            Loginclear();
            RoomChooseclear();
            Roomclear();
            Deskclear();
            pane.remove(pane.getComponentAt(600 * base, 0));
            pane.remove(roomnameL);

        } catch (Exception e)
        {

        }
    }
}
